Added trace output to many classes.
[lwes-dotnet/github-mirror.git] / Test Projects / Org.Lwes.Tests / EventListenerTests.cs
blob30b8ea245d0f2e80b34790e9ace00ab22f1305e2
1 namespace Org.Lwes.Tests
3 using System;
4 using System.Net;
5 using System.Threading;
7 using Microsoft.VisualStudio.TestTools.UnitTesting;
9 using Moq;
11 using Org.Lwes.Emitter;
12 using Org.Lwes.Listener;
14 [TestClass]
15 public class EventListenerTests
17 #region Methods
20 [TestMethod]
21 public void AccessDefaultListener()
23 var e = new
25 EventName = "UserLogin",
26 Attributes = new
28 UserName = new { Name = "username", Token = TypeToken.STRING, Value = "bob" },
29 Password = new { Name = "password", Token = TypeToken.UINT64, Value = 0xfeedabbadeadbeefUL },
30 ClientIP = new { Name = "clientIP", Token = TypeToken.IP_ADDR, Value = IPAddress.Parse("127.0.0.1") },
31 Successful = new { Name = "successful", Token = TypeToken.BOOLEAN, Value = false }
34 bool done = false;
35 Event receivedEvent = default(Event);
37 // Mock an IEventSink that records the incomming event...
38 Mock<IEventSink> mock = new Mock<IEventSink>();
39 mock.Setup(framework => framework.HandleEventArrival(It.IsAny<ISinkRegistrationKey>(), It.IsAny<Event>()))
40 .Callback<ISinkRegistrationKey, Event>((k, ev) =>
42 receivedEvent = ev;
43 done = true;
44 });
46 Event sourceEvent;
47 using (IEventListener listener = EventListener.CreateDefault())
49 Assert.IsNotNull(listener);
50 listener.RegisterEventSink(mock.Object).Activate();
52 IEventEmitter emitter = EventEmitter.CreateDefault();
53 Assert.IsNotNull(emitter);
55 // Create the event...
56 sourceEvent = new Event(e.EventName);
57 sourceEvent.SetValue(e.Attributes.UserName.Name, e.Attributes.UserName.Value);
58 sourceEvent.SetValue(e.Attributes.Password.Name, e.Attributes.Password.Value);
59 sourceEvent.SetValue(e.Attributes.ClientIP.Name, e.Attributes.ClientIP.Value);
60 sourceEvent.SetValue(e.Attributes.Successful.Name, e.Attributes.Successful.Value);
62 // give the round-trip 20 seconds to complete, this is extremely generous...
63 long time_out_ticks = DateTime.Now.Ticks + TimeSpan.FromSeconds(20).Ticks;
65 while (!done && DateTime.Now.Ticks < time_out_ticks)
67 emitter.Emit(sourceEvent);
68 Thread.Sleep(1000);
71 Assert.IsTrue(done, "Should have received an event");
72 Assert.AreEqual(sourceEvent[e.Attributes.UserName.Name].GetValue<string>(), receivedEvent[e.Attributes.UserName.Name].GetValue<string>());
73 Assert.AreEqual(sourceEvent[e.Attributes.Password.Name].GetValue<ulong>(), receivedEvent[e.Attributes.Password.Name].GetValue<ulong>());
74 Assert.AreEqual(sourceEvent[e.Attributes.ClientIP.Name].GetValue<IPAddress>(), receivedEvent[e.Attributes.ClientIP.Name].GetValue<IPAddress>());
75 Assert.AreEqual(sourceEvent[e.Attributes.Successful.Name].GetValue<bool>(), receivedEvent[e.Attributes.Successful.Name].GetValue<bool>());
76 Console.Write(receivedEvent.ToString());
79 #endregion Methods